<?php

	/**
	 * 根据字段获取站点参数值
	 * @param unknown $fields
	 */
	function getFields($fields){
		$Field = M('Fields');
		$str = '';
		$fs = $Field->where('flag = 1')->select();
		foreach ($fs as $key=>$val){
			if($val['field'] == $fields){
				$str = $val['value'];
			}
		}
		return $str;
	}
	
	/**
	 * 写入日志
	 * Enter description here ...
	 * @param unknown_type $title
	 * @param unknown_type $msg
	 */
	function writeLog($msg){
		$file = fopen($_SERVER['DOCUMENT_ROOT'].__ROOT__."/Log/".Date('Y-m-d').".log", "a+");
		$msg .= "\r\n";
		fwrite($file, $msg);
		fclose($file);
	}
	
	/**
	 * 验证电话号码
	 * Enter description here ...
	 */
	function checkPhone($tel){
		if(preg_match("/1[34578]{1}\d{9}$/",$tel)){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 格式化金钱
	 * @param unknown $price
	 */
	function formatPrice($price){
		return sprintf("%.2f", $price);
	}
	
	/**
	 * 截取字符串
	 */
	function subString($str,$num=0){
		if(strlen($str) > $num){
			return mb_substr($str, 0 ,$num ,'utf-8')."...";
		}else{
			return $str;
		}
	}
	
	/**
	 * 隐藏手机号中间四位
	 */
	function getHidePhone($tel){
		return substr($tel,0,3)."***".substr($tel,-4);
	}
	
	/**
	 * [移动端访问自动切换主题模板]
	 * @return boolen [是否为手机访问]
	 */
	function ismobile() {
		// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
		if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
			return true;
	
		//此条摘自TPM智能切换模板引擎，判断是否为客户端
		if(isset ($_SERVER['HTTP_CLIENT']) &&'PhoneClient'==$_SERVER['HTTP_CLIENT'])
			return true;
		//如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
		if (isset ($_SERVER['HTTP_VIA']))
			//找不到为flase,否则为true
			return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
		//判断手机发送的客户端标志,兼容性有待提高
		if (isset ($_SERVER['HTTP_USER_AGENT'])) {
			$clientkeywords = array(
					'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile'
			);
			//从HTTP_USER_AGENT中查找手机浏览器的关键字
			if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
				return true;
			}
		}
		//协议法，因为有可能不准确，放到最后判断
		if (isset ($_SERVER['HTTP_ACCEPT'])) {
			// 如果只支持wml并且不支持html那一定是移动设备
			// 如果支持wml和html但是wml在html之前则是移动设备
			if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 订单数量回补
	 * 多个订单code用逗号隔开
	 * @param string $code
	 */
	function backProductNum($code = ""){
		$OrderDetails = M('OrderDetails');$Product = M('Product');
		if(!empty($code)){
			$querySql = 'SET num = CASE id ';
			$map['order_code'] = array('in',$code);
			$list = $OrderDetails->where($map)->field('product_id,num')->select();
			foreach ($list as $key=>$val){
				//组装补回数量sql
				$queryIds .= $val['product_id'].',';
				$querySql .= ' WHEN '.$val['product_id'].' THEN num +'.$val['num']." ";
			}
			$querySql .= ' END ';
			$queryIds = substr($queryIds,0,strlen($queryIds)-1);
			$querySql .= 'where id in ('.$queryIds.')';
			$result = $Product->execute('update __TABLE__ '.$querySql);
			if($result){
				return true;
			}else{
				return false;
			}
		}
	}

function checkStop(){
	echo '<p style="text-align:center;margin-top: 50px;font-size: 25px;">无权限操作!</p>';
	exit;
}
	
